export const dateFormate = {
	// 获取当前时间
	getNowTime: () => {
		var nowDate = new Date();
		var year = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
		var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
		return year + "-" + month + "-" + date;
	},

	// 获取当前时间的昨天
	getZuoTian() {
		var day1 = new Date();
		day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
		let month = day1.getMonth() + 1;
		var s1 =
			day1.getFullYear() +
			"-" +
			(month < 10 ? "0" + month : month) +
			"-" +
			(day1.getDate() < 10 ? "0" + day1.getDate() : day1.getDate());
		return s1
	},

	// 获取当前日期的上一个月
	getlastMonth() {
		let now = new Date();
		// 当前月的日期
		let nowDate = now.getDate();
		let lastMonth = new Date(now.getTime());
		// 设置上一个月（这里不需要减1）
		lastMonth.setMonth(lastMonth.getMonth());
		// 设置为0，默认为当前月的最后一天
		lastMonth.setDate(0);
		// 上一个月的天数
		let daysOflastMonth = lastMonth.getDate();
		// 设置上一个月的日期，如果当前月的日期大于上个月的总天数，则为最后一天 
		lastMonth.setDate(nowDate > daysOflastMonth ? daysOflastMonth : nowDate);
		return lastMonth;
	},

	// 格式化任意时间
	formatDate: (time : string, n : number) => {
		var date : any = ''
		if (time) {
			date = new Date(time);
		} else {
			date = new Date();
		}

		var year = date.getFullYear(),
			month = date.getMonth() + 1, //月份是从0开始的
			day = date.getDate(),
			hour = date.getHours(),
			min = date.getMinutes(),
			sec = date.getSeconds();

		if (n == 1) {
			var newTime =
				year +
				"-" +
				(month < 10 ? "0" + month : month) +
				"-" +
				(day < 10 ? "0" + day : day) +
				" " +
				(hour < 10 ? "0" + hour : hour) +
				":" +
				(min < 10 ? "0" + min : min) +
				":" +
				(sec < 10 ? "0" + sec : sec);
			return newTime;
		} else {
			var newTime =
				year +
				"-" +
				(month < 10 ? "0" + month : month) +
				"-" +
				(day < 10 ? "0" + day : day)
			return newTime;
		}
	},

	// 获取任意时间的几天前或者几天后
	getRenYiTime(time : string, n : number, m : number, p : number) {
		if (n == 1) {
			let s1 : any = new Date(time);
			s1 = s1.setDate(s1.getDate() + m);
			s1 = this.formatDate(s1, p)
			return s1
		} else if (n == 0) {
			let s1 : any = new Date(time);
			s1 = s1.setDate(s1.getDate() - m);
			s1 = this.formatDate(s1, p)
			return s1
		}
	},

	// 获取当前时间所在周的开始结束日期
	getNowWeekSE() {
		var date = new Date();
		const year = date.getFullYear();
		const month = date.getMonth();
		const day = date.getDate();
		const week = date.getDay();
		let a = new Date(year, month, day - week + 1);
		let b = new Date(year, month, day - week + 7);
		return a + '-' + b
	},

	// 获取当前时间所在月的开始结束日期
	getNowMonthSE: () => {
		var date = new Date()
		const year = date.getFullYear();
		const month = date.getMonth();
		const day = date.getDate();
		let n = new Date(year, month, 1);
		let m = new Date(year, month + 1, 0);
		return n + '-' + m
	},

	// 获取当前时间所在季度的开始结束日期
	getNowJiDu: () => {
		var now = new Date(); // 当前日期
		var nowDayOfWeek = now.getDay(); // 今天是本周的第几天
		var nowDay = now.getDate(); // 当前日
		var nowMonth = now.getMonth(); // 当前月
		var nowYear = now.getFullYear(); // 当前年
		nowYear += nowYear < 2000 ? 1900 : 0;

		var quarterStartMonth = 0;
		if (nowMonth < 3) {
			quarterStartMonth = 0;
		}
		if (2 < nowMonth && nowMonth < 6) {
			quarterStartMonth = 3;
		}
		if (5 < nowMonth && nowMonth < 9) {
			quarterStartMonth = 6;
		}
		if (nowMonth > 8) {
			quarterStartMonth = 9;
		}

		var monthStartDate : any = new Date(nowYear, quarterStartMonth, 1);
		var monthEndDate : any = new Date(nowYear, quarterStartMonth + 1, 1);
		var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);

		var quarterStartDate = new Date(nowYear, quarterStartMonth, 1);

		var quarterEndDate = new Date(
			nowYear,
			quarterStartMonth + 2,
			days
		);

		return quarterStartDate + '-' + quarterEndDate
	},

	// 获取当前时间所在年的开始结束时间
	getNowYearSE(n : number) {
		var startTime = new Date();
		startTime.setDate(1);
		startTime.setMonth(0);
		var endTime = new Date();
		endTime.setFullYear(endTime.getFullYear() + 1);
		endTime.setMonth(0);
		endTime.setDate(0);
		var sMonth =
			startTime.getMonth() + 1 < 10 ?
				"0" + (startTime.getMonth() + 1) :
				startTime.getMonth() + 1;
		var sDay =
			startTime.getDate() < 10 ?
				"0" + startTime.getDate() :
				startTime.getDate();
		var eMonth =
			endTime.getMonth() + 1 < 10 ?
				"0" + (endTime.getMonth() + 1) :
				endTime.getMonth() + 1;
		var eDay =
			endTime.getDate() < 10 ? "0" + endTime.getDate() : endTime.getDate();
		var startParam =
			"START_DATE==" +
			startTime.getFullYear() +
			"-" +
			sMonth +
			"-" +
			sDay +
			" 00:00:00";
		var endParam =
			"END_DATE==" +
			endTime.getFullYear() +
			"-" +
			eMonth +
			"-" +
			eDay +
			" 23:59:59";

		if (n == 1) {
			let arr = []

			arr[0] = startTime.getFullYear() +
				"-" +
				sMonth +
				"-" +
				sDay

			arr[1] = endTime.getFullYear() +
				"-" +
				eMonth +
				"-" +
				eDay
			return arr
		} else {
			let arr = []
			arr[0] = startTime.getFullYear() +
				"-" +
				sMonth +
				"-" +
				sDay +
				" 00:00:00";

			arr[1] = endTime.getFullYear() +
				"-" +
				eMonth +
				"-" +
				eDay +
				" 23:59:59";
			return arr
		}
	},

	// 获取任意时间几个月或者几个月后的时间 几个月前传正数 几个月后传负数
	getMonthsAgo(monthNum : any) {
		var date = new Date();
		var year : any = date.getFullYear();
		var month : any = date.getMonth() + 1;
		month = month < 10 ? "0" + month : month;
		var day : any = date.getDate();
		var days : any = new Date(year, month, 0);
		day = day < 10 ? "0" + day : day;
		//days = days.getDate();
		var year2 : any = year;
		var month2 : any = parseInt(month) - monthNum;
		if (month2 <= 0) {
			var a = Math.abs(month2); //取绝对值
			year2 =
				parseInt(year2) -
				parseInt(
					(monthNum / 12 <= 1 ?
						1 :
						a >= 12 ?
							Math.ceil(parseInt(monthNum) / 12) :
							Math.floor(parseInt(monthNum) / 12)) as any
				);
			month2 = 12 - (Math.abs(month2) % 12);
		}
		var day2 = day;
		var days2 : any = new Date(year2, month2, 0);
		days2 = days2.getDate(); //获取当前日期中月的天数 2月只有28
		if (day2 > days2) {
			day2 = days2;
		}
		if (month2 < 10) {
			month2 = "0" + month2;
		}
		var t2 = year2 + "-" + month2 + "-" + day2 + " 00:00:00";
		return t2;
	},

	getDateRange(type : number) {
		const now = new Date()

		// 格式化日期为 yyyy/MM/dd
		const formatDate = (date : any) => {
			const y = date.getFullYear()
			const m = String(date.getMonth() + 1).padStart(2, '0')
			const d = String(date.getDate()).padStart(2, '0')
			return `${y}/${m}/${d}`
		}

		if (type === 1) {
			return formatDate(now) // 今天
		}

		if (type === 2) {
			const yesterday = new Date(now)
			yesterday.setDate(now.getDate() - 1)
			return formatDate(yesterday) // 昨天
		}

		if (type === 3) {
			const today = new Date(now)
			const day = today.getDay() || 7 // 周日=7，周一=1
			const monday = new Date(today)
			monday.setDate(today.getDate() - (day - 1))

			const todayStr = formatDate(today)
			const mondayStr = formatDate(monday)

			return todayStr === mondayStr ? todayStr : `${mondayStr}~${todayStr}`
		}

		if (type === 4) {
			const today = new Date(now)
			const firstDay = new Date(today.getFullYear(), today.getMonth(), 1)

			const todayStr = formatDate(today)
			const firstStr = formatDate(firstDay)

			return todayStr === firstStr ? todayStr : `${firstStr}~${todayStr}`
		}

		return ''
	}

}